package com.example.mapp.buildSql;

import com.example.entity.SpotCheckCache;
import com.example.entity.SpotCheckItem;
import com.example.util.StringUtil;

import java.util.Map;

/**
 * 监管场所自定义查询导出
 */
public class SpotCheckItemMappSql {
    /**
     *
     * 查询的SQL
     * findList
     * @param
     * @return
     */
    public String findlists(SpotCheckItem spotCheckItem){
        StringBuffer sb = new StringBuffer();
        sb.append(" select ");
        sb.append("  id AS id, ");
        sb.append("  item_id AS 'itemId', ");
        sb.append("  plan_id AS 'planId', ");
        sb.append("  custom_name AS 'customName', ");
        sb.append("  is_custom AS 'isCustom' ");
        sb.append("  from spot_check_item  where 1=1 ");
        if(spotCheckItem.getPlanId() !=null){
            sb.append("  and plan_id ="+spotCheckItem.getPlanId());
        }
        return sb.toString();
    }

    /**
     * 查询检查项
     */

    public String findListExcel(SpotCheckItem spotCheckItem){
        StringBuffer sb = new StringBuffer();
        sb.append(" select ");
        sb.append("  check_man as 'checkMan', ");
        sb.append("  item_content as 'itemContent' ");
        sb.append("  from spot_object_plan plan  ");
        sb.append("  left join spot_check_item em on plan.plan_id = em.plan_id  ");
        sb.append("  left join check_item c ON em.item_id = c.id  ");
        sb.append("  where plan.id="+spotCheckItem.getObjPlanId());
        sb.append("  and em.plan_id ="+spotCheckItem.getPlanId());
        return sb.toString();
    }

    /**
     * App端查询检查项数据
     *
     */
    public String findCheckItemApp(Map map){
        StringBuffer sb = new StringBuffer();
        sb.append(" select ");
        sb.append("  (case when che.item_id is not null then ite.item_content else che.custom_name end) as 'checkName', ");
        sb.append("  che.plan_id                                                                        as planId, ");
        sb.append("  che.id                                                                             as id, ");
        sb.append("  (case when ite.ext2 is not null then ite.ext2 else '自定义维保' end)                as 'ext2', ");
        sb.append("  ite.id                                                                             as iteId ");
        sb.append("  from spot_check_item che ");
        sb.append("  left join check_item ite ON che.item_id = ite.id ");
        sb.append("  where che.plan_id ="+map.get("planId"));
        return sb.toString();
    }

    /**
     * App端获取电梯信息
     *
     */
    public String findElevatorInfoApp(Map map){
        StringBuffer sb = new StringBuffer();
        sb.append(" select ");
        sb.append("  tor.elevator_num as 'elevatorNum',");
        sb.append("  tor.build_addr as 'buildAddr',");
        sb.append("  type.type_name as 'typeName' , ");
        sb.append("  tor.maintenance_party_id as 'maintenancePartyId',");

        sb.append("  tor.used_party_id as 'usedPartyId',");
        sb.append("  p.party_name AS 'maintenanceName',");
        sb.append("  y.party_name as 'usedName'");

        sb.append("  from elevator tor ");
        sb.append("  left join elevator_type type ON tor.elevator_type_id = type.id ");
        sb.append("  left join party p ON p.id=tor.maintenance_party_id");
        sb.append("  left join party y ON y.id = tor.used_party_id ");
        sb.append("  where tor.id='"+map.get("elevatorId")+"'");
        sb.append("  and   (tor.maintenance_user_first='"+map.get("mantanceUserId")+"'"+" OR tor.maintenance_user_second='"+map.get("mantanceUserId")+"'"+")");
        return sb.toString();
    }

    /**
     * App端多个电梯的缓存表
     *
     */
    public String returnFindCache(SpotCheckCache cache){
        StringBuffer sb = new StringBuffer();
        sb.append(" select ");
        sb.append("  plan_id as 'planId', ");
        sb.append("  name as 'name', ");
        sb.append("  check_item_id as 'checkItemId', ");
        sb.append("  create_by as 'createBy', ");
        sb.append("  spot_check_item_id as 'spotCheckItemId', ");
        sb.append("  check_name as 'checkName', ");
        sb.append("  remarkes as 'remarkes', ");
        sb.append("  pic_url as 'picUrl', ");
        sb.append("  implement_object_id as 'implementObjectId', ");
        sb.append("  (case when item.ext2 is not null then item.ext2 else '自定义维保' end )as 'ext2', ");
        sb.append("  result_evaluate as 'resultEvaluate', ");
        sb.append("  opinion as 'opinion', ");
        sb.append("  is_checked as 'isChecked'");

        sb.append("  from spot_check_cache che ");
        sb.append("  left join check_item item ON item.id = che.check_item_id ");
        sb.append("  where 1=1 ");

        if(cache.getPlanId() !=null ){
            sb.append("  and plan_id="+cache.getPlanId());
        }
        if(StringUtil.isNotBlank(cache.getCreateBy())){
            sb.append("  and create_by='"+cache.getCreateBy()+"'");
        }
        if(StringUtil.isNotBlank(cache.getName())){
            sb.append("  and name='"+cache.getName()+"'");
        }
        if(cache.getImplementObjectId() !=null){
            sb.append("  and implement_object_id="+cache.getImplementObjectId());
        }

        return sb.toString();
    }


}
